package com.cjj.thread.sms_send;

/**
 * @ClassName BatachThread
 * @Date 2021/06/28 23:38:38
 * @Created by ccc-j
 * @email ccc-ju@outlook.com
 */

import com.cjj.thread.ListUtils;
import com.cjj.thread.entity.UserEntity;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 多线程处理数据
 */
public class BatachThread {

    public static void main(String[] args) {
        // 1.初始化用户
        List<UserEntity> userEntities = initUser();
        // 2.定义每个线程最多跑多少数据
        int userCount = 200;
        // 3.计算线程数并且计算每个线程跑多少数据
        List<List<UserEntity>> lists = ListUtils.splitList(initUser(), userCount);
        for(int i = 0; i < lists.size(); i++){
            List<UserEntity> entityList = lists.get(i);
            // 4.让子线程分批异步处理数据
            new UserThread(entityList).start();
            //System.out.println("i:" + i + "--" + entityList.toString());
        }
    }

    public static List<UserEntity> initUser(){
        List<UserEntity> userEntities = new ArrayList<>();
        for(int i = 1; i <= 1000; i++){
            userEntities.add(UserEntity.builder().userId("id:" + i).userName("name:" + i).build());
        }
        return userEntities;
    }

}
@Data
class UserThread extends Thread {

    List<UserEntity> list;

    public UserThread(List<UserEntity> list){
        this.list = list;
    }

    @Override
    public void run() {
        for(UserEntity entity : list){
            /*try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // none
            }*/
            System.out.println("name:" + getName() + "--" + entity.toString());
        }
    }
}